<template>
    <common-dialog :dialogVisible="dialogVisible" width="520px" :title="'线下模板导入'" @cancel="cancel" @ok="ok">
       <common-upload ref="upload" :paramsVisible="false" :multiple="false" :supportTypes="['.xls', '.xlsx']"
        @handleUpload="handleUpload"></common-upload>
    </common-dialog>
</template>
<script>
//  oid='3680723'
//  :fileTypeDefault="'合约'" :fileAddressDefault='fileAddressDefault'
import commonDialog from '@/components/common/commonDialog'
import commonUpload from '@/components/common/commonUpload'
import { uploadCommon } from'@/api/projectPlan.js'
let userinfo = JSON.parse(sessionStorage.getItem('userInfo'))
import { api } from '@/api/projectPlan.js'
export default {
  components: { commonDialog, commonUpload },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    dialogVisible(val) {
      if (val && this.$refs.upload) {
        this.$refs.upload.fileList = [] // 将选中的置空
      }
    }
  },
  data() {
    return {
      fileAddressDefault: 'OR:wt.folder.SubFolder:3680710',
      oid: '',
      action: api.importPIPlanActivities, // 上传接口地址
      params: null, // 上传参数
      // fileList: [], // 上传文件列表
      file: null // 上传文件
    }
  },
  mounted() {
    this.oid = sessionStorage.getItem('oid')
    this.params = {
      "oid": `OR:com.pisx.pmgt.project.PIProject:${this.oid}`,
      "userId": userinfo.loginName
    }
  },
  methods: {
    // 上传文件回调
    handleUpload(file) {
      this.file = file
      // this.fileList = [...this.fileList, file];
    },
    // 选择文件类型回调
    selectFileType(info) {
      console.log(info)
      this.$set(this.params, 'fileType', info)
    },
    // 选择文件存放地址回调
    selectFileAddress(info) {
      console.log(info)
      this.$set(this.params, 'fileAddress', info.join('/'))
    },
    cancel() {
      this.$emit('cancel')
    },
    ok() {
      // 上传
      let params = new FormData()
      for(let key in this.params) {
        params.append(key, this.params[key])
      }
      params.append('_file', this.file)
      uploadCommon(this.action, params).then(({data}) => {
        if (data.success === "true") {
          this.$message.success(data.message)
        } else {
          this.$message.error(data.message || '上传失败')
        }
        this.$emit('ok', data.success === "true" || data.success === true)
      })
    }
  }
}
</script>
<style lang="less" scoped>
  .my-dialog{
    /deep/ .ant-modal-body{
      padding: 20px 71px;
    }
  }
  
</style>